import { ColorConfig } from "../../engine/ColorConfig";
import DateUtil from "../../engine/DateUtil";
import { Logger } from "../../engine/Logger";
import { SpriteFrameComponent } from "../../engine/SpriteFrameComponent";
import ViewBase from "../../engine/ViewBase";
import GameController from "../../GameController";
import { GameConfig } from "../config/GameConfig";
import { RoomModel } from "../model/RoomModel";
import { RoomPlayer } from "../model/RoomPlayer";
import { VideoInfo } from "../model/VideoInfo";
import { UIHelper } from "../util/UIHelper";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewGameRecordDetail extends ViewBase {

    private defaultItemNode: cc.Node;

    private videoInfoList: Array<VideoInfo> = []

    public onInitChild(resultList: Array<VideoInfo>) {
        this.videoInfoList = resultList;
        Logger.log("ViewGameRecordDetail=", this.videoInfoList)
        this.defaultItemNode = this.findChild(this.node, "scrollView/view/content/defaultItemNode")
        this.refresh()
    }



    private refresh() {
        this.hideAllChildren(this.defaultItemNode.parent)
        for (let i = 0; i < this.videoInfoList.length; i++) {
            let videoInfo: VideoInfo = this.videoInfoList[i]
            let itemNode: cc.Node = this.defaultItemNode.parent.children[i]
            if (!itemNode) {
                itemNode = cc.instantiate(this.defaultItemNode)
                itemNode.parent = this.defaultItemNode.parent
            }
            itemNode.active = true;
            let txtJu: cc.Label = this.getChild(itemNode, "txtJu", cc.Label)
            txtJu.string = "第" + videoInfo.ju + "局"
            let txtTime: cc.Label = this.getChild(itemNode, "txtTime", cc.Label)
            txtTime.string = DateUtil.formatDateStr(videoInfo.time)
            let txtRoomId: cc.Label = this.getChild(itemNode, "txtRoomId", cc.Label)
            txtRoomId.string = videoInfo.gameResult.room_id + ""
            let scoreList: Array<number> = videoInfo.videoResult["scoreList"]
            for (let j = 0; j < 4; j++) {
                let txtName: cc.Label = this.getChild(itemNode, "txtName" + j, cc.Label)
                let txtId: cc.Label = this.getChild(itemNode, "txtId" + j, cc.Label)
                let txtScore: cc.Label = this.getChild(itemNode, "txtScore" + j, cc.Label)
                let player: RoomPlayer = videoInfo.playerList[j]
                if (player) {
                    txtName.string = player.nick_name
                    txtId.string = "ID:" + player.userid
                    if (scoreList[j] >= 0) {
                        txtScore.string = "+" + player.score;
                        txtScore.font = txtScore.getComponent(SpriteFrameComponent).fontList[0]
                    } else {
                        txtScore.string = "-" + player.score;
                        txtScore.font = txtScore.getComponent(SpriteFrameComponent).fontList[1]
                    }
                }
                else {
                    txtId.string = ""
                    txtName.string = ""
                    txtScore.string = ""
                }
            }
            let btnHuifang: cc.Node = this.findChild(itemNode, "btnHuifang")
            UIHelper.onAddEvent(btnHuifang, () => {
                this.closeView()
                GameController.instance.playVideo(videoInfo)
            })
        }
    }


}
